package com.capt_2;

import java.util.Random;

public class Equation2 {
    static String[] exerice = new String[50];
    static int[] exericeint = new int[50];
    public static void main(String[] args) {
        /**
         * 1,产生50个算式
         * 2,操作数和结果在0-100
         * 3，模块化设计
         */
        pringHeader();
        gentateEquation();
        printExercise();
        printResult();
    }

    private static void printResult() {
//        模块四,打印答案
        System.out.println();
        System.out.println("----------------------------------------");
        System.out.println("---------------习题答案如下---------------");
        System.out.println("----------------------------------------");
        for (int i=0;i<exericeint.length;i++){
            System.out.print("第"+(i+1)+"题答案: " + exericeint[i]);
            if ((i+1)%5 == 0){
                System.out.println();
            }
        }
    }

    private static void printExercise() {
//        模块三,打印习题集:
        for (int i=0;i<exerice.length;i++){
            System.out.print("第"+(i+1)+"题: " + exerice[i]);
            if ((i+1)%5 == 0){
                System.out.println();
            }
        }
    }

    private static void gentateEquation() {
//        产生50个算式
        int leftNumber,rightNumber,resout;
        char op = '+';

        Random random = new Random();
        for (int i=0;i<50;i++){
//            采用无限循环确保获取到的数据准确
            do {
                leftNumber = random.nextInt(101);
                rightNumber = random.nextInt(101);
                op = random.nextInt(2)==0?'+':'-';
                if (random.nextInt(2) == 0){
                    op = '+';
                    resout = leftNumber + rightNumber;
                }else {
                    op = '-';
                    resout = leftNumber - rightNumber;
                }
            }while (!(resout >=0 && resout <= 100));


            // 将算式拼接并且保存到数组中.
            exerice[i] = ""+leftNumber+op+rightNumber+"=  ";
            exericeint[i] = resout;
        }

    }

    private static void pringHeader() {
//        打印头部
        System.out.println("----------------------------------------");
        System.out.println("-----------产生50个随机加减法算式-----------");
        System.out.println("----------------------------------------");
        System.out.println();

    }
}
